package org.dromara.run.controller;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.poi.ss.formula.functions.T;
import org.dromara.common.core.validate.OtherGroup;
import org.dromara.run.domain.bo.ScheduleBo;
import org.dromara.run.domain.bo.ScoreBo;
import org.dromara.run.domain.vo.ScoreVo;
import org.dromara.run.service.IScheduleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.run.domain.vo.RunListVo;
import org.dromara.run.domain.bo.RunListBo;
import org.dromara.run.service.IRunListService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 阳光跑名单
 *
 * @author chl
 * @date 2024-08-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/run/runList")
public class RunListController extends BaseController {

    private final IRunListService runListService;
    private final IScheduleService scheduleService;
    /**
     * 查询阳光跑名单列表
     */
    @SaCheckPermission("run:runList:list")
    @GetMapping("/list")
    public TableDataInfo<RunListVo> list(RunListBo bo, PageQuery pageQuery) {
        return runListService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询阳光跑名单列表
     *
     * @param bo 查询主体
     */
    @SaCheckPermission("run:runList:query")
    @GetMapping("/infoList")
    public R<List<RunListVo>> getInfoList(RunListBo bo) {
        return R.ok(runListService.queryList(bo));
    }

    /**
     * 导出阳光跑名单列表
     */
    @SaCheckPermission("run:runList:export")
    @Log(title = "阳光跑名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RunListBo bo, HttpServletResponse response) {
        List<RunListVo> list = runListService.queryList(bo);
        AtomicInteger index = new AtomicInteger(1);
        list.forEach(vo-> {
            // 序号递增
            vo.setIndex(index.getAndIncrement());
        });
        ExcelUtil.exportExcel(list, "阳光跑名单", RunListVo.class, response);
    }

    /**
     * 获取阳光跑名单详细信息
     *
     * @param listId 主键
     */
    @SaCheckPermission("run:runList:query")
    @GetMapping("/{listId}")
    public R<RunListVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long listId) {
        return R.ok(runListService.queryById(listId));
    }

    /**
     * 新增阳光跑名单
     */
    @SaCheckPermission("run:runList:add")
    @Log(title = "阳光跑名单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RunListBo bo) {
        return toAjax(runListService.insertByBo(bo));
    }

    /**
     * 修改阳光跑名单
     */
    @SaCheckPermission("run:runList:edit")
    @Log(title = "阳光跑名单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RunListBo bo) {
        return toAjax(runListService.updateByBo(bo));
    }

    /**
     * 删除阳光跑名单
     *
     * @param listIds 主键串
     */
    @SaCheckPermission("run:runList:remove")
    @Log(title = "阳光跑名单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{listIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] listIds) {
        RunListVo runListVo = runListService.queryById(listIds[0]);

        Boolean aBoolean = runListService.deleteWithValidByIds(List.of(listIds), true);
        if (!aBoolean) {
            return R.fail("操作失败");
        }
        if (runListVo!=null) {
            RunListBo runListBo=new RunListBo();
            runListBo.setScheduleId(runListVo.getScheduleId());
            runListBo.setTenantId(runListVo.getTenantId());
            List<RunListVo> runLists= runListService.queryList(runListBo);
            if (runLists.size()<0) {
                ScheduleBo scheduleBo=new ScheduleBo();
                scheduleBo.setScheduleId(Long.valueOf(runListVo.getScheduleId()));
                scheduleBo.setStatus("0");
                Boolean aBoolean1 = scheduleService.updateByBo(scheduleBo);
                if (!aBoolean1) {
                    return R.fail("操作失败");
                }
            }
        }
        return toAjax(runListService.deleteWithValidByIds(List.of(listIds), true));
    }

    /**
     * 生成阳光跑名单
     */
    @SaCheckPermission("run:runList:add")
    @Log(title = "阳光跑名单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("batchCreate")
    public R<T> batchCreateRunList(@Validated(OtherGroup.class) @RequestBody ScheduleBo bo) {
        if(!runListService.batchCreateRunList(bo)){
            R.fail("生成失败");
        }
        return R.ok(bo.getScheduleId().toString());
    }
}
